<!doctype html>
<html lang="en">
<head>
	<title>Shader - Explorer (Three.js)</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<link rel=stylesheet href="css/base.css"/>
</head>
<body>

<script src="js/Three.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/THREEx.KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>

<!-- Note the additional JS files! always need all of these for postprocessing. -->
<script src="js/postprocessing/EffectComposer.js"></script>
<script src="js/postprocessing/RenderPass.js"></script>
<script src="js/postprocessing/ShaderPass.js"></script>
<script src="js/postprocessing/MaskPass.js"></script>
<script src="js/shaders/CopyShader.js"></script>

<!-- separate out the shaders specific to this demo -->
<script src="js/shaders/DotScreenShader.js"></script>
<script src="js/postprocessing/DotScreenPass.js"></script>

<script src="js/shaders/BleachBypassShader.js"></script>
<script src="js/shaders/ColorifyShader.js"></script>
<script src="js/shaders/ConvolutionShader.js"></script>
<script src="js/shaders/DotScreenShader.js"></script>
<script src="js/shaders/FilmShader.js"></script>
<script src="js/shaders/HorizontalBlurShader.js"></script>
<script src="js/shaders/SepiaShader.js"></script>
<script src="js/shaders/VerticalBlurShader.js"></script>
<script src="js/shaders/VignetteShader.js"></script>

<script src="js/postprocessing/BloomPass.js"></script>
<script src="js/postprocessing/FilmPass.js"></script>
<script src="js/postprocessing/DotScreenPass.js"></script>
<script src="js/postprocessing/TexturePass.js"></script>


<!-- GUI for experimenting with shaders -->		
<script type='text/javascript' src='js/DAT.GUI.min.js'></script>

<!-- jQuery code to display an information button and box when clicked. -->
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui.js"></script>
<link rel=stylesheet href="css/jquery-ui.css" />
<link rel=stylesheet href="css/info.css"/>
<script src="js/info.js"></script>
<div id="infoButton"></div>
<div id="infoBox" title="Demo Information">
This three.js demo is part of a collection at
<a href="http://stemkoski.github.io/Three.js/">http://stemkoski.github.io/Three.js/</a>
</div>
<!-- ------------------------------------------------------------ -->

<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
<script>
/*
	Three.js "tutorials by example"
	Author: Lee Stemkoski
	Date: July 2013 (three.js v59dev)
*/

// MAIN

// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube, gui;
var shaderActive = "none";

// variables for shader
var composer, finalPass;

init();
animate();

// FUNCTIONS 		
function init() 
{
	// SCENE
	scene = new THREE.Scene();
	// CAMERA
	var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
	var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
	camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
	scene.add(camera);
	camera.position.set(0,150,400);
	camera.lookAt(scene.position);	
	// RENDERER
	if ( Detector.webgl )
		renderer = new THREE.WebGLRenderer( {antialias:true} );
	else
		renderer = new THREE.CanvasRenderer(); 
	renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	container = document.getElementById( 'ThreeJS' );
	container.appendChild( renderer.domElement );
	// EVENTS
	THREEx.WindowResize(renderer, camera);
	THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
	// CONTROLS
	controls = new THREE.OrbitControls( camera, renderer.domElement );
	// STATS
	stats = new Stats();
	stats.domElement.style.position = 'absolute';
	stats.domElement.style.bottom = '0px';
	stats.domElement.style.zIndex = 100;
	container.appendChild( stats.domElement );
	// LIGHT
	var light = new THREE.PointLight(0xffffff);
	light.position.set(0,250,0);
	scene.add(light);
	// FLOOR
	var floorTexture = new THREE.ImageUtils.loadTexture( 'images/checkerboard.jpg' );
	floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; 
	floorTexture.repeat.set( 10, 10 );
	var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
	var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
	var floor = new THREE.Mesh(floorGeometry, floorMaterial);
	floor.position.y = -0.5;
	floor.rotation.x = Math.PI / 2;
	scene.add(floor);
	
	// SKYBOX/FOG
	var materialArray = [];
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-xpos.png' ) }));
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-xneg.png' ) }));
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-ypos.png' ) }));
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-yneg.png' ) }));
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-zpos.png' ) }));
	materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'images/dawnmountain-zneg.png' ) }));
	for (var i = 0; i < 6; i++)
	   materialArray[i].side = THREE.BackSide;
	var skyboxMaterial = new THREE.MeshFaceMaterial( materialArray );
	
	var skyboxGeom = new THREE.CubeGeometry( 5000, 5000, 5000, 1, 1, 1 );
	
	var skybox = new THREE.Mesh( skyboxGeom, skyboxMaterial );
	scene.add( skybox );	

	
	////////////
	// CUSTOM //
	////////////
	var cubeGeometry = new THREE.CubeGeometry( 50, 50, 50 );
	var crateTexture = new THREE.ImageUtils.loadTexture( 'images/crate.gif' );
	var crateMaterial = new THREE.MeshBasicMaterial( { map: crateTexture } );
	cube = new THREE.Mesh( cubeGeometry, crateMaterial );
	cube.position.set(-50,26,0);
	scene.add(cube);
	
	var sphereGeometry = new THREE.SphereGeometry( 50, 32, 16 ); 
	var sphereMaterial = new THREE.MeshLambertMaterial( {color: 0x8888ff} ); 
	var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
	sphere.position.set(50, 50, -50);
	scene.add(sphere);

	
	/////////////////////
	// POST-PROCESSING //
	/////////////////////
	
	// GUI setup
	
	gui = new dat.GUI();
	
	parameters = 
	{
		x: 0, y: 30, z: 0,
		sepiaAmount: 0.9,
		vignetteOffset: 1.5, vignetteDarkness: 1.6,
		dotscreenAngle: 0.5, dotscreenScale: 0.8,
		bloomStrength: 1.3, bloomFactor: 1.0,
		horizBlur: 1.25, vertiBlur: 1.25,
		useShaderNone:      function() { setupShaderNone();      },
		useShaderSepia:     function() { setupShaderSepia();     },
		useShaderVignette:  function() { setupShaderVignette();  },
		useShaderDotScreen: function() { setupShaderDotScreen(); },
		useShaderBloom:     function() { setupShaderBloom();     },
		useShaderBlur:      function() { setupShaderBlur();      }
		
	};
	
	gui.add( parameters, 'useShaderNone' ).name("Display Original Scene");
	
	var folderSepia = gui.addFolder('Sepia');
	var sepiaAmountGUI = folderSepia.add( parameters, 'sepiaAmount' ).min(0).max(1).step(0.01).name("Amount").listen();
	sepiaAmountGUI.onChange(
		function(value)
		{   setupShaderSepia();   }
	);
	folderSepia.add( parameters, 'useShaderSepia' ).name("Use Sepia Shader");
	folderSepia.open();
	
	var folderVignette = gui.addFolder('Vignette');
	var vignetteOffsetGUI = folderVignette.add( parameters, 'vignetteOffset' ).min(0).max(3).step(0.01).name("Offset").listen();
	vignetteOffsetGUI.onChange(
		function(value)
		{   setupShaderVignette();   }
	);
	var vignetteDarknessGUI = folderVignette.add( parameters, 'vignetteDarkness' ).min(0).max(3).step(0.01).name("Darkness").listen();
	vignetteDarknessGUI.onChange(
		function(value)
		{   setupShaderVignette();   }
	);
	folderVignette.add( parameters, 'useShaderVignette' ).name("Use Vignette Shader");
	folderVignette.open();
	
	var folderDotScreen = gui.addFolder('Dot Screen');
	var dotScreenAngleGUI = folderDotScreen.add( parameters, 'dotscreenAngle' ).min(0).max(3.14).step(0.01).name("Angle").listen();
	dotScreenAngleGUI.onChange(
		function(value)
		{   setupShaderDotScreen();   }
	);
	var dotScreenScaleGUI = folderDotScreen.add( parameters, 'dotscreenScale' ).min(0).max(2).step(0.01).name("Scale").listen();
	dotScreenScaleGUI.onChange(
		function(value)
		{   setupShaderDotScreen();   }
	);
	folderDotScreen.add( parameters, 'useShaderDotScreen' ).name("Use DotScreen Shader");
	folderDotScreen.open();
	
	var folderBloom = gui.addFolder('Bloom');
	var bloomStrengthGUI = folderBloom.add( parameters, 'bloomStrength' ).min(0).max(3).step(0.01).name("Strength").listen();
	bloomStrengthGUI.onChange(
		function(value)
		{   setupShaderBloom();   }
	);
	var bloomFactorGUI = folderBloom.add( parameters, 'bloomFactor' ).min(0.1).max(2.0).step(0.01).name("Factor").listen();
	bloomFactorGUI.onChange(
		function(value)
		{   setupShaderBloom();   }
	);
	folderBloom.add( parameters, 'useShaderBloom' ).name("Use Bloom Shader");
	folderBloom.open();
	
	var folderBlur = gui.addFolder('Blur');
	var horizBlurGUI = folderBlur.add( parameters, 'horizBlur' ).min(0.0).max(3.0).step(0.01).name("Horizontal Blur").listen();
	horizBlurGUI.onChange(
		function(value)
		{   setupShaderBlur();   }
	);
	var vertiBlurGUI = folderBlur.add( parameters, 'vertiBlur' ).min(0.0).max(3.0).step(0.01).name("Vertical Blur").listen();
	vertiBlurGUI.onChange(
		function(value)
		{   setupShaderBlur();   }
	);
	folderBlur.add( parameters, 'useShaderBlur' ).name("Use Blur Shader");
	folderBlur.open();
	
	setupShaderNone();
	
	
}

function setupShaderNone()
{
	shaderActive = "none";
}

function setupShaderSepia()
{
	composer = new THREE.EffectComposer( renderer );
	composer.addPass( new THREE.RenderPass( scene, camera ) );
	
	var shaderSepia = THREE.SepiaShader;
	var effectSepia = new THREE.ShaderPass( shaderSepia );
	effectSepia.uniforms[ "amount" ].value = parameters.sepiaAmount;
	effectSepia.renderToScreen = true;
	composer.addPass(effectSepia);
	shaderActive = "sepia";	
}

function setupShaderVignette()
{
	composer = new THREE.EffectComposer( renderer );
	composer.addPass( new THREE.RenderPass( scene, camera ) );
	
	var shaderVignette = THREE.VignetteShader;
	var effectVignette = new THREE.ShaderPass( shaderVignette );
	// larger values = darker closer to center
	// darkness < 1  => lighter edges
	effectVignette.uniforms[ "offset" ].value = parameters.vignetteOffset;
	effectVignette.uniforms[ "darkness" ].value = parameters.vignetteDarkness;
	effectVignette.renderToScreen = true;
	composer.addPass(effectVignette);
	shaderActive = "vignette";
}

function setupShaderDotScreen()
{
	composer = new THREE.EffectComposer( renderer );
	composer.addPass( new THREE.RenderPass( scene, camera ) );
	
	var effectDotScreen = new THREE.DotScreenPass( new THREE.Vector2(0,0), parameters.dotscreenAngle, parameters.dotscreenScale ); 
	effectDotScreen.renderToScreen = true; 
	composer.addPass(effectDotScreen); 
	shaderActive = "dotscreen";
}

function setupShaderBloom()
{
	composer = new THREE.EffectComposer( renderer );
	composer.addPass( new THREE.RenderPass( scene, camera ) );
	
	// https://github.com/mrdoob/three.js/issues/1065
	// this method changes default kernel/sigma values by the same factor
	var effectBloom = new THREE.BloomPass( parameters.bloomStrength, parameters.bloomFactor * 25, parameters.bloomFactor * 4.0 );
	composer.addPass( effectBloom );
	
	var effectCopy = new THREE.ShaderPass( THREE.CopyShader );
	effectCopy.renderToScreen = true;
	composer.addPass( effectCopy );
	shaderActive = "bloom";
}

function setupShaderBlur()
{
	composer = new THREE.EffectComposer( renderer );
	composer.addPass( new THREE.RenderPass( scene, camera ) );
	
	// http://devmaster.net/posts/3100/shader-effects-glow-bloom	
	// http://www.neilblevins.com/cg_education/specular_bloom/specular_bloom.htm
	var effectHorizBlur = new THREE.ShaderPass( THREE.HorizontalBlurShader );
	var effectVertiBlur = new THREE.ShaderPass( THREE.VerticalBlurShader );
	effectHorizBlur.uniforms[ "h" ].value = parameters.horizBlur / window.innerWidth;
	effectVertiBlur.uniforms[ "v" ].value = parameters.vertiBlur / window.innerHeight;
	effectVertiBlur.renderToScreen = true;
	
	composer.addPass( effectHorizBlur );
	composer.addPass( effectVertiBlur );
	shaderActive = "blur";
}

function animate() 
{
    requestAnimationFrame( animate );
	render();		
	update();
}

function update()
{
	if ( keyboard.pressed("z") ) 
	{ 
		// do something
	}
	
	controls.update();
	stats.update();
}

function render() 
{
	if ( shaderActive == "none" )
		renderer.render( scene, camera );
	else
		composer.render();
}

</script>

</body>
</html>
